<%@page contentType="text/html; charset=utf-8"%>

<div class="easyui-layout" data-options="fit: true">
	<div data-options="region: 'north', border: false" style="height: 40px; padding:5px 5px; overflow: hidden">
		<div class="search-bar">
			<div class="bar-row">
				<div class="bar-input">
					<input name="quickSearch" class="easyui-searchbox" data-options="searcher:quickSearch, width: 150, height: 25,prompt: '快速搜索...'" />
           		</div>
			</div>
		</div>
				
    </div>
	<div data-options="region: 'center', border: false" style="border-top: 1px solid #ddd;">
		<div id="dg_dialog"></div>
    </div>
</div>


<script>
	var $dg_dialog, ids="${ids}", auth="" ;
	$(function(){
		//如果是单个用户，则获取该用户所拥有的角色
		if(ids.split(",").length == 1) {
			$.post("${ctx}/admin/sys/auth/get", {"userId": ids}, function(result){
				auth = result ;
			});
		}
		
		$dg_dialog = $("#dg_dialog").datagrid({
	        url: "${ctx}/admin/sys/role/datagrid",
	        idField: "id", fit: true, 
	        border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
	        pagination: true, rownumbers: true, toolbar: "#toolbars_dialog", remoteSort: true, 
	        sortName: 'createTime', sortOrder: 'desc',
	        frozenColumns: [[
	            { field: "ck", checkbox: true }
	        ]],
	        columns: [[
	            { field: "name", title: "角色名称", width: 180 },
	            { field: "identity", title: "角色标识", width: 180 },
	            { field: "description", title: "角色描述", width: 250 }
	        ]],
	        onLoadSuccess: function(){
	        	$.fn.datagrid.extensions.onLoadSuccess.apply(this, arguments);
	        	$dg_dialog.datagrid('unselectAll');$dg_dialog.datagrid('clearSelections');
	        	
	        	if(null != auth && "" != auth) {
	        		var roleIds = auth.roleIds.split(",") ;
	        		for(var i=0; i<roleIds.length; i++) {
		        		$dg_dialog.datagrid("selectRecord", roleIds[i]);
	        		}
	        			
	        	}
	        },
	        enableHeaderClickrole: true, enableHeaderContextrole: true, enableRowContextrole: false
	    });
	});
	function reload(){$dg_dialog.datagrid("reload") ;}
	function quickSearch(value,name){
		var o = {} ; o[name] = value ; $dg_dialog.datagrid("load",o);
	}
	function getIds() {
		var rows = $dg_dialog.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					ids.push(rows[i].id);
				}
			}
		}
		return ids ;
	}	
	
	//提交表单数据
	var submitNow = function($d, $dg, flag) {
		$.post("${ctx}/admin/sys/auth/allotUserRole", {"userIds": ids, "roleIds": getIds().join(",")}, function(result) {
			if (result.status) {
				alertify.success(result.message);$.easyui.loaded();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.message, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	};
	
	//验证表单
	var submitForm = function($d, $dg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $dg, flag) ;
		}
	};
	
</script>

